package com.sjzd.server.controller;

import com.sjzd.server.pojo.Admin;
import com.sjzd.server.pojo.AdminLoginParam;
import com.sjzd.server.pojo.RespBean;
import com.sjzd.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 *  登录controller
 * @author ZFQ
 * @date 2021/2/22
 */
@Api(tags = "LoginController")
@RestController
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value="登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest req) {
        String verifyCode = (String)req.getSession().getAttribute("verifyCode");
        if (!StringUtils.hasText(verifyCode) || !verifyCode.equalsIgnoreCase(adminLoginParam.getVerifyCode())) {
            return RespBean.error("验证码错误,请重新输入！", null);
        }
        return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword(), req);
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("注销成功！");
    }

}
